/*
 * @Author: 许警 11378748333@
 * @Date: 2024-06-14 17:12:25
 * @LastEditors: 许警 11378748333@
 * @LastEditTime: 2025-05-20 15:41:35
 * @FilePath: /hsxlsystem/src/hooks/useSelects.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { ref, reactive, watch } from 'vue'
// isFree true是前端自己处理全选 false是后端返回全部id
export default function useSelects({ rowSelectionOption, key = 'id', isFree = false }) {
  const renderData = ref([]) // isFree false时 后端传来的全部id
  // const idLog = ref([]) // isFree true 前端记录的曾经查看过的页面id
  // const selectStatus = false // true 全选  false反选
  const rowKey = ref(key) // id主健值
  // 默认配置
  const rowSelection = reactive({
    checkStrictly: false,
    selectedRowKeys: [],
    selectStatus: false, // true 全选  false反选 isFree为true时生效
    reverseSelection: [], // 全选状态下 要加入不选择的id
    nowPageId: [],
    changePageId: arr => {
      if (isFree) {
        rowSelection.nowPageId = arr
        if (rowSelection.selectStatus) {
          arr.forEach(item => {
            console.log('进入1', item)

            if (!rowSelection.reverseSelection.includes(item)) {
              console.log('进入', item)
              rowSelection.selectedRowKeys.push(item)
            }
          })
        }
      }
    },
    selections: [
      {
        key: 'all',
        text: '全部选中',
        onSelect: changableRowKeys => {
          if (isFree == false) {
            console.log(renderData.value)
            rowSelection.selectedRowKeys = JSON.parse(JSON.stringify(renderData.value))
            console.log(rowSelection.selectedRowKeys)
          } else {
            rowSelection.selectStatus = true
            rowSelection.selectedRowKeys = [...new Set([...rowSelection.selectedRowKeys, ...rowSelection.nowPageId])]
            rowSelection.reverseSelection = []
            console.log('rowSelection.selectedRowKeys', rowSelection.selectedRowKeys)
          }
        }
      },
      {
        key: 'cancel',
        text: '全部取消',
        onSelect: changableRowKeys => {
          if (isFree == true) {
            rowSelection.selectStatus = false
            rowSelection.reverseSelection = []
          }
          rowSelection.selectedRowKeys = []

          // rowSelection.selectedRowKeys = [...new Set([...rowSelection.selectedRowKeys, ...data.list.map(item=>item[rowKey.value])])]
        }
      }
    ],
    onSelect: (record, selected, selectedRows) => {
      console.log(rowKey.value, '选择多项')
      // console.log(record['nurseId'], '选择多项')
      if (selected) {
        rowSelection.selectedRowKeys.push(record[rowKey.value])

        if (rowSelection.selectStatus) {
          rowSelection.reverseSelection = rowSelection.reverseSelection.filter(item => {
            if (record[rowKey.value] !== item) {
              return item
            }
          })
          console.log(rowSelection.reverseSelection)
        }
      } else {
        rowSelection.selectedRowKeys = rowSelection.selectedRowKeys.filter(item => {
          if (record[rowKey.value] !== item) {
            return item
          }
        })

        if (rowSelection.selectStatus) {
          rowSelection.reverseSelection.push(record[rowKey.value])
        }
      }

      console.log(rowSelection.selectedRowKeys)
    },

    onSelectAll: async (selected, selectedRows, changeRows) => {
      console.log(selected, selectedRows, changeRows)
      let arr = changeRows.map(item => item[rowKey.value])
      if (selected) {
        rowSelection.selectedRowKeys = [...new Set([...rowSelection.selectedRowKeys, ...arr])]
        if (rowSelection.selectStatus) {
          rowSelection.reverseSelection = rowSelection.reverseSelection.filter(item => {
            if (!arr.includes(item)) {
              return item
            }
          })
          console.log(rowSelection.reverseSelection)
        }
      } else {
        rowSelection.selectedRowKeys = rowSelection.selectedRowKeys.filter(item => {
          console.log(item)
          if (!arr.includes(item)) {
            return item
          }
        })
        if (rowSelection.selectStatus) {
          arr.map(item => {
            if (!rowSelection.reverseSelection.includes(item)) {
              rowSelection.reverseSelection.push(item)
              console.log('item111', rowSelection.reverseSelection)
            }
          })
          console.log(rowSelection.reverseSelection)
        }
      }
      // 全选情况下
    }
  })

  // 合并配置
  Object.assign(rowSelection, rowSelectionOption)

  return {
    renderData,
    rowSelection
  }
}
